const app = getApp()
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
import api from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
  },
  
  //兑换记录
  async getList () {
    let data={}
    data['flag'] = 'gift_malls_jilu'
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      result.forEach((item,index) => {
        item.giftimg = api.checkurl(item.giftimg)
      })
      this.setData({
        list:result,
      })
    
    }else {
      Toast(message)
    }
  },

  //核销
  hexiao:api.throttle(function (e) {
    let _this = this
    wx.requestSubscribeMessage({
      tmplIds: ['KG5VGQD6jj3gOUvSOdwRCr4rp432QpOIBV7wpol0dqY'],
      success (res) { 
        console.log(res)
        _this.hexiao1(e)
      },
      fail(res){
        console.log(res)
        _this.hexiao1(e)
      }
    })
  }),

  hexiao1(e){
    let _this = this
    let receid = e.currentTarget.dataset.id
    let giftid = e.currentTarget.dataset.code
    //核销---扫描二维码后 跳转到该页面 "/pages/my_integral_log_success/index?receiveId=" + receid
    wx.scanCode({
      onlyFromCamera: true,
      success (res) {
        console.log(res)
        //https://youthclubtest.digilinx.net.cn/youthclub_qyh/saoma_new.php?type_id=260&&type=present_hx_tw
        let url = '/'+res.path
        let ipos = url.indexOf('scene')
        if( ipos>-1 ){
          let obj = api.decodeurl(url.substr(ipos+6))
          if( !obj['id'] ){
            Toast('二维码格式不正确')
            return
          }
          if( giftid!=obj['id'] ){
            Toast('商品不正确')
            return
          }
          url = '/pages/my_integral_log_success/index?receiveId='+receid
        }else{
          Toast('二维码数据不正确')
          return
        }
        console.log(url)
        wx.navigateTo({
          url
        })
      }
    })
  },
  
  //发送核销
  sendhx(data){

  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.receiveId){
      this.receive(options.receiveId)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})